package com.common.proxy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

public class ReadConfig {

	/**
	 *
	 * @param fileName
	 * @return
	 */
	public Properties init(String fileName) {
		Properties prop= new Properties();
		try {
			InputStream is = getClass().getResourceAsStream(fileName);
			prop.load(is);
			if(is!=null){
				is.close();
			}
		}
		catch(Exception e) {
			System.out.println(e+"file "+fileName+" not found");
		}

		return prop;
	}

	/**
	 * ���������ļ�,λ��Ϊ��  WebRoot/WEB-INF/classes/auto/create/  ����
	 * @param path    �� WebRoot/ or request.getSession().getServletContext().getRealPath("/")
	 * @param fileName �� "db.properties"
	 * @param prop
	 * @param comment
	 * @return
	 */
	public int store(String path, String fileName, Properties prop, String comment) {

		OutputStream out = null;
		InputStream in = null;
		int flag =0;
//		try {
//
//			String newFilePath = path ; // + Constants.FILEPATH;
//			//String newFilePath = path + Constants.FILEPATH;   // "WEB-INF"+separator+"classes"+separator+"auto"+separator+"creat"+separator;
//			//��ʱȫ��ʹ��ͳһ�����֣����ǳ���
//			if(fileName==null||fileName.equals("")){
//				fileName = Constants.FILENAME;
//			}else{
//				fileName = Constants.FILENAME;
//			}
//
//			String newFileName = newFilePath + fileName;
//			System.out.println("---------stor "+ newFileName+" start-------------");
//
//			//String newFileName = path + fileName;
//			File file = new File(newFileName);
//			if(file.exists()){
//				System.out.println("---------"+newFileName+" is exists-------------");
//			}else{
//				System.out.println("---------"+newFileName+"  is not exists-------------");
//				file.createNewFile();
//			}
//
//			// д�ļ�ʱ���Ը��ǵķ�ʽд��� ,����ԭ�ļ����ݶ����� ,ԭ�ļ������ݾͻᶪʧ
//			// ����Ҫ��ԭ�ļ�����Ϣ�ȶ�����,Ȼ�����������ֵ���,�µ��Զ������ϵ�,
//			//Ȼ�󱣴�,�ͻ����ֵ(ԭ������ͬ��key��),��ԭ�������ڵ�key��value ���浽proerties��
//			System.out.println("...............111111.............."+newFileName+" new properties -------------");
//			prop.list(System.out);
//
//			in = new FileInputStream(newFileName);
//			Properties oldprop = new Properties();
//			oldprop.load(in);
//			in.close();
//			System.out.println("................222222............."+newFileName+" old properties -------------");
//			oldprop.list(System.out);
//
//			System.out.println("................333333.............."+newFileName+" before reset, old properties + new properties -------------");
//			oldprop.putAll(prop);
//			oldprop.list(System.out);
//
//			out = new FileOutputStream(newFileName);
//			oldprop.store(out, comment);
//			System.out.println(".................444444.............."+newFileName+" after reset properties -------------");
//			oldprop.list(System.out);
//			out.close();
//			flag = 1;
//			System.out.println("---------stor "+ newFileName+" complete-------------");
//		}
//		catch(Exception e) {
//			System.out.println(e+"file "+fileName+" not found");
//		}finally{
//			if(in!=null){
//				try {
//					in.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//			if(out!=null){
//				try {
//					out.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
		return flag;
	}

	/**
	 * ���������ļ�,λ��Ϊ��ǰ·�����ϼ��� src/auto/create/  ����
	 * @param fileName  �� "src/auto/creat/db.properties"
	 *  or  request.getSession().getServletContext().getRealPath("/") + "properties1.properties"
	 * @return
	 */
	public int storeToSrc(String path, String fileName, Properties prop, String comment) {

		OutputStream out = null;
		InputStream in = null;
		int flag =0;
		try {

			File file = new File(path + fileName);
			if(file.exists()){
				System.out.println("---------"+fileName+" is exists-------------");
			}else{
				System.out.println("---------"+fileName+"  is not exists-------------");
				file.createNewFile();
			}
			//��ȡ�ϼ��ļ����
			String parentFileName = file.getParent();
			File parentFile = file.getParentFile().getParentFile();
			String currentPath = file.getPath();
			String separator = System.getProperty("file.separator");
			String srcPath= separator + "src"+separator+"auto"+separator+"creat"+separator;
			String newFileName = parentFile.getPath() +srcPath + fileName;

			System.out.println("---------parentFileName="+parentFileName+"-------------");
			System.out.println("---------parentFile="+parentFile+"-------------");
			System.out.println("---------currentPath="+currentPath+"-------------");
			System.out.println("---------newFileName="+newFileName+"-------------");

			File cuurentfile = new File(newFileName);
			if(cuurentfile.exists()){
				System.out.println("---------new file  "+cuurentfile+" is exists-------------");
			}else{
				System.out.println("---------new file  "+cuurentfile+"  is not exists-------------");
				cuurentfile.createNewFile();
			}

			// д�ļ�ʱ���Ը��ǵķ�ʽд��� ,����ԭ�ļ����ݶ����� ,ԭ�ļ������ݾͻᶪʧ
			// ����Ҫ��ԭ�ļ�����Ϣ�ȶ�����
			in = new FileInputStream(newFileName);
			prop.load(in);

			out = new FileOutputStream(newFileName);
			prop.store(out, comment);
			prop.list(System.out);
//			if(in!=null){
//			in.close();
//			}
			if(out!=null){
				out.close();
			}
			flag = 1;

			System.out.println("---------"+newFileName+"-------------");
		}
		catch(Exception e) {
			System.out.println(e+"file "+fileName+" not found");
		}
		return flag;
	}

	/**
	 * ���������ļ�Ϊxml
	 * @param fileName �� "src/auto/creat/db.xml"
	 * @return
	 */
	public int storeToXml(String path, String fileName, Properties prop, String comment) {

		OutputStream out = null;
		InputStream in = null;
		int flag =0;
		try {

			File file = new File(path + fileName);
			if(file.exists()){
				System.out.println("---------"+fileName+" is exists-------------");
			}else{
				System.out.println("---------"+fileName+"  is not exists-------------");
				file.createNewFile();
			}
			//��ȡ�ϼ��ļ����
			String parentFileName = file.getParent();
			File parentFile = file.getParentFile().getParentFile();
			String currentPath = file.getPath();
			String separator = System.getProperty("file.separator");
			String srcPath= separator + "src"+separator+"auto"+separator+"creat"+separator;
			String newFileName = parentFile.getPath() +srcPath + fileName;

			System.out.println("---------parentFileName="+parentFileName+"-------------");
			System.out.println("---------parentFile="+parentFile+"-------------");
			System.out.println("---------currentPath="+currentPath+"-------------");
			System.out.println("---------newFileName="+newFileName+"-------------");

			File cuurentfile = new File(newFileName);
			if(cuurentfile.exists()){
				System.out.println("---------new file  "+cuurentfile+" is exists-------------");
			}else{
				System.out.println("---------new file  "+cuurentfile+"  is not exists-------------");
				cuurentfile.createNewFile();
			}

			// д�ļ�ʱ���Ը��ǵķ�ʽд��� ,����ԭ�ļ����ݶ����� ,ԭ�ļ������ݾͻᶪʧ
			// ����Ҫ��ԭ�ļ�����Ϣ�ȶ�����
			in = new FileInputStream(newFileName);
			//prop.loadFromXML(in);

			out = new FileOutputStream(newFileName);
			prop.storeToXML(out, comment, "GBK");
			//prop.storeToXML(out, comment);
			prop.list(System.out);
			if(in!=null){
				in.close();
			}
			if(out!=null){
				out.close();
			}

			flag = 1;
			System.out.println("---------"+newFileName+"-------------");

		}
		catch(Exception e) {
			e.printStackTrace();
			System.out.println("---------"+e+"file "+fileName+" not found");
		}

		return flag;
	}

	public int storeSimple(String path, String fileName, Properties prop, String comment) {

		OutputStream out = null;
		InputStream in = null;
		int flag =0;
		try {

			File file = new File(path + fileName);
			if(file.exists()){
				System.out.println("---------"+fileName+" is exists-------------");
			}else{
				System.out.println("---------"+fileName+"  is not exists-------------");
				file.createNewFile();
			}

			// д�ļ�ʱ���Ը��ǵķ�ʽд��� ,����ԭ�ļ����ݶ����� ,ԭ�ļ������ݾͻᶪʧ
			// ����Ҫ��ԭ�ļ�����Ϣ�ȶ�����
			in = new FileInputStream(path + fileName);
			prop.load(in);

			out = new FileOutputStream(path + fileName);
			prop.store(out, comment);
			prop.list(System.out);
			if(out!=null){
				out.close();
			}
			flag = 1;

			System.out.println("---------"+path + fileName+"-------------");
		}
		catch(Exception e) {
			System.out.println(e+"file "+fileName+" not found");
		}
		return flag;
	}

	/**
	 * ���������ļ�Ϊxml
	 * @param fileName �� "src/auto/creat/db.xml"
	 * @return
	 */
	public int storeToXmlSimple(String path, String fileName, Properties prop, String comment) {

		int flag =0;
		try {
			FileOutputStream out = new FileOutputStream(path +fileName);
			prop.storeToXML(out, comment, "GBK");
			prop.list(System.out);
			out.close();
			if(out!=null){
				out.close();
			}
			flag = 1;
		}
		catch(Exception e) {
			System.out.println(e+"file "+fileName+" not found");
		}

		return flag;
	}

	/**
	 *
	 */
	public static void getSysInfo() {

		Properties properties = new Properties();

		// ͨ����ϵͳ���Թ��������� properties
		properties = System.getProperties();
		properties.list(System.out);
	}

	// �ļ�λ��:��ǰpackage
	public void loadFile() {
		try {
			Properties properties = new Properties();
			properties.load(getClass().getResourceAsStream("test.property"));
			System.err.println(properties.getProperty("key1")+properties.getProperty("key2"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Properties loadFileByIO(String fileName) {
		Properties properties = new Properties();
		InputStream in = null;
		try {
			in = new FileInputStream(fileName); //"TestSource/com/vs/test/test.property"
			properties.load(in);
			//properties.list(System.out);
			//System.err.println(properties.getProperty("key1")+properties.getProperty("key2"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

	public void loadXmlFileByIO() {

		InputStream in = null;

		try {

			in = new FileInputStream("/auto/creat/db.properties");
			Properties properties = new Properties();
			properties.loadFromXML(in);
			properties.list(System.out);
			System.err.println(properties.getProperty("key1")+properties.getProperty("key2"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void storeXmlFileByIO() {

		OutputStream out = null;
		InputStream in = null;
		try {

			Properties properties = new Properties();
			properties.setProperty("test", "testStore");

			// д�ļ�ʱ���Ը��ǵķ�ʽд��� ,����ԭ�ļ����ݶ����� ,ԭ�ļ������ݾͻᶪʧ
			// ����Ҫ��ԭ�ļ�����Ϣ�ȶ�����
			in = new FileInputStream("test.xml");
			properties.loadFromXML(in);

			out = new FileOutputStream("test.xml");
			properties.storeToXML(out, "writeToXml");
			properties.list(System.out);
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void storeFileByIO() {

		OutputStream out = null;
		InputStream in = null;
		try {

			Properties properties = new Properties();
			properties.setProperty("test", "testStore");

			// д�ļ�ʱ���Ը��ǵķ�ʽд��� ,����ԭ�ļ����ݶ����� ,ԭ�ļ������ݾͻᶪʧ
			// ����Ҫ��ԭ�ļ�����Ϣ�ȶ�����
			in = new FileInputStream("src/auto/creat/db.properties");
			properties.load(in);

			out = new FileOutputStream("src/auto/creat/db.properties");
			properties.store(out, "writeToXml");
			properties.list(System.out);
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}


	}

	public static void main(String[] args){

		System.out.println("---------------------");
//		String path ="src/auto/creat/";
//		String fileName = "db.properties";
//		String xmlFileName ="src/auto/creat/db.xml";
//		String comment ="test";
//
//		Properties prop = new Properties();
//		prop.setProperty("one", "buckle my shoe");
//		prop.setProperty("two", "shut the door");
//		prop.setProperty("4444", "pick up sticks");
//		prop.setProperty("test", "lay them straight");
//		prop.setProperty("oracle", "a big, fat hen");
//		prop.setProperty("55555555", "55555555555");
//		ReadConfig test = new ReadConfig();
//		test.store(path, fileName, prop, comment);
		//test.storeToXml(path, xmlFileName, prop, comment);

		String fileName = "weblogic.properties";
		ReadConfig test = new ReadConfig();
		Properties prop = new Properties();
		prop = test.init(fileName);
		System.out.println("----------prop="+prop);
	}
}
